package com.youlemall.shop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.youlemall.common.pojo.PageResult;
import com.youlemall.pojo.Goods;
import com.youlemall.service.GoodsService;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

/**商品类控制器
 * 2 * @Author: kevin
 * 3 * @Date: 2019/7/19 21:06
 * 4
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

	@Reference(timeout = 100000)
	private GoodsService goodsService;


	/**
	 *商品类添加
	 * @param goods
	 * @return
	 */
	@RequestMapping("/save")
	public boolean save(@RequestBody Goods goods){
		try {
			//获取登录用户名
			String sellerId = SecurityUtils.getSubject()
					.getPrincipal().toString();
			//设置商家id
			goods.setSellerId(sellerId);
			goodsService.save(goods);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 商品管理分页查询功能
	 * @param page
	 * @param rows
	 * @param goods
	 * @return
	 */
	@GetMapping("/findByPage")
	public PageResult findByPage(Integer page ,
								 @RequestParam(defaultValue = "10")Integer rows,
								 Goods goods){
		String sellerId = SecurityUtils.getSubject().getPrincipal().toString();
		goods.setSellerId(sellerId);
		return goodsService.findByPage(goods,page,rows);
	}

	@GetMapping("/updateMarketable")
	public boolean updateMarketable(Long[] ids,String status){
		try {
			goodsService.updateStatus("is_marketable",ids,status);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}
